package com.cooper.service.general;

import java.util.Collections;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.cooper.dao.general.AdviceReplyDAO;
import com.cooper.dao.general.AdviceSubjectDAO;
import com.cooper.entity.AdviceReplyEntity;
import com.cooper.entity.AdviceSubjectEntity;
import com.cooper.pagination.PageInfo;
import com.cooper.pagination.PageResult;
import com.cooper.vo.AdviceSearchVo;

@Service
public class AdviceService {
	
	@Autowired
	private AdviceReplyDAO adviceReplyDao;
	
	@Autowired
	private AdviceSubjectDAO adviceSubjectDao;
	
	public Long saveSubject(AdviceSubjectEntity entity) throws Exception{
		return this.adviceSubjectDao.saveEntity(entity);
	}
	
	public Long saveReply(AdviceReplyEntity entity) throws Exception{
		return this.adviceReplyDao.saveEntity(entity);
	}

	@SuppressWarnings("unchecked")
	public PageResult<AdviceSubjectEntity> queryForList(AdviceSearchVo searchVo, PageInfo pageInfo) throws Exception{
		int rowCount = this.adviceSubjectDao.querySubjectCount(searchVo);
		pageInfo.setRowCount(rowCount);
		if(0 == rowCount){
			return new PageResult<AdviceSubjectEntity>(Collections.EMPTY_LIST, pageInfo);
		}
		List<AdviceSubjectEntity> dataList = this.adviceSubjectDao.querySubjectList(searchVo, pageInfo);
		return new PageResult<AdviceSubjectEntity>(dataList, pageInfo);
	}
	
	public List<AdviceReplyEntity> getReplyList(long subjectId) throws Exception{
		return this.adviceReplyDao.getReplyList(subjectId);
	}

}
